import cv2
import os


def resize_image_opencv(image_path, output_path, scale_factor):
    img = cv2.imread(image_path)

    # 获取原始尺寸
    height, width = img.shape[:2]

    # 计算新的尺寸
    new_width = int(width * scale_factor)
    new_height = int(height * scale_factor)

    # 按比例缩放
    img_resized = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LANCZOS4)

    # 保存图片
    cv2.imwrite(output_path, img_resized)
    print(f"Image saved as {output_path} with size {new_width}x{new_height}")


def get_jpg_images(folder_path):
    images = [f for f in os.listdir(folder_path) if f.lower().endswith('.jpg')]
    return images


# source_image_dir = "/Users/daxiang/Downloads/qiqi"
# target_image_dir = "/Users/daxiang/Downloads/qiqi/初修"

source_image_dir = "/Users/daxiang/Downloads/qiqi/初修"
target_image_dir = "/Users/daxiang/Downloads/qiqi_mini/初修"

source_images = get_jpg_images(source_image_dir)

for source_image in source_images:
    input_path = f"{source_image_dir}/{source_image}"
    output_path = f"{target_image_dir}/{source_image}"
    resize_image_opencv(input_path, output_path, scale_factor=0.1)